package com.shqcjd.yunweiyanoperator.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.model.pojo.VideoCategory;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.shqcjd.yunweiyanoperator.util.UIUtils;

import java.util.ArrayList;
import java.util.List;

public class ShareDialog extends Dialog {
    private Button btn_dialog_send;
    private TextView tv_dialog_share_title;
    private EditText et_dialog_share_info, et_dialog_share_remark;
    private Context context;
    private setOnClickDialogListener listener;
    private TagLayout tl_dialog_share;
    private List<VideoCategory> mList;
    private String mTypeId = "0";

    public void setOnClickDialogListener(setOnClickDialogListener listener){
        this.listener = listener;
    }

    public ShareDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public ShareDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ShareDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_share);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        btn_dialog_send = findViewById(R.id.btn_dialog_send);
        et_dialog_share_info = findViewById(R.id.et_dialog_share_info);
        tv_dialog_share_title = findViewById(R.id.tv_dialog_share_title);
        tl_dialog_share = findViewById(R.id.tl_dialog_share);
        et_dialog_share_remark = findViewById(R.id.et_dialog_share_remark);

        btn_dialog_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String info = et_dialog_share_info.getText().toString().trim();
                if(info.equals("")){
                    ToastUtils.showShort(context, "请输入标题");
                    return;
                }
                if(mTypeId.equals("0")){
                    ToastUtils.showShort(context, "请选择分类");
                    return;
                }
                if(mTypeId.equals("-1")&&et_dialog_share_remark.getText().toString().equals("")){
                    ToastUtils.showShort(context, "请添加备注");
                    return;
                }
                listener.onClickAdd(info, mTypeId, et_dialog_share_remark.getText().toString().trim());
            }
        });


//        tl_dialog_share.resetCheckedState();
//        tl_dialog_share.setCheckedType(TagLayout.CHECKED_TYPE.SINGLE);

    }

    public void cleanData(){
        et_dialog_share_info.setText("");
    }

    public void setTitle(String title){
        tv_dialog_share_title.setText(title);
    }

    public void setVideoData(List<VideoCategory> mList){
        this.mList = mList;
        String[] tagText = new String[mList.size()+1];
        tagText[0] = "";
            for(int i=1; i<mList.size()+1; i++){
                tagText[i] = mList.get(i-1).name;
            }
        addTag(tl_dialog_share, tagText);
    }
    public interface setOnClickDialogListener{
        void onClickAdd(String info, String mTypeId, String remark);
    }

    private void addTag(final TagLayout tagLayout, String[] tagText) {
        for (int i = 0; i < tagText.length; i++) {
            String t = tagText[i];
            final TagTextView tagView = new TagTextView(context, tagLayout);
            // layout parameters
            ViewGroup.MarginLayoutParams lp;

            if (i == 0) {
                // title
                tagView.setTextColor(0xff333333);
                tagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);

                lp = new ZLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);

            } else {


            tagView.setEllipsize(TextUtils.TruncateAt.END);
            tagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            tagView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.background_tag));
            tagView.setTextColor(Color.parseColor("#666666"));
                tagView.setPadding(25, 15, 25, 15);
                tagView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tagView.setChecked(!tagView.isChecked());
                        if(tagLayout.value().equals("其它  ")){
                            et_dialog_share_remark.setVisibility(View.VISIBLE);
                        }else{
                            et_dialog_share_remark.setVisibility(View.GONE);
                        }
                        if(tagView.isChecked()){
                            mTypeId = mList.get((Integer) tagView.getTag()-1).id;
                        }else{
                            mTypeId = "0";
                        }
                    }
                });

                lp = new ZLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            }
            tagView.setText(t);
            tagView.setTag(i);
            tagView.setSingleLine(true);

            lp.leftMargin = 20;
            lp.topMargin = 10;
            lp.rightMargin = 0;
            lp.bottomMargin = 0;
            tagLayout.addView(tagView, lp);
        }
    }
}
